<?php
class TSanitize
{
	protected $req = array();

	public function __construct(Array $req){
		$this->req = $req;
	}

	public function get($ky, $sanitize){
		if(isset($this->req[$ky])){
			if($sanitize == true){
				return $this->_sanit($this->req[$ky]);
			}else{
				return $this->req[$ky];
			}
		}
		return null;
	}

	protected function _sanit($val){
		if(is_array($val)){
			foreach($val as $k => $v){
				$val[$k] = $this->_cnvParm($v,$k);
			}
		}else{
			$val = $this->_cnvParm($v);
		}
		return $val;
	}

	protected function _cnvParm($val,$key=""){
		if (is_null($val))	return $val;
		if ($val == "")		return $val;
		if ($key == "__utma")		return $val;
		if ($key == "__utmb")		return $val;
		if ($key == "__utmc")		return $val;
		if ($key == "__utmz")		return $val;
		if ($key == "PHPSESSID")	return $val;

		$val = str_replace("\0", "", $val);
		$val = preg_replace("[\x01-\x20]", "", $val);

		$val = preg_replace("[\|\~]", "", $val);
		$val = preg_replace("[\&]", "＆", $val);
		$val = preg_replace("[\;]", "；", $val);
		$val = preg_replace("[:]", "：", $val);
		$val = preg_replace("[\?]", "？", $val);
		$val = preg_replace("[\,]", "，", $val);
		$val = preg_replace("[\"]", "”", $val);
		$val = preg_replace("[\']", "’", $val);
		$val = preg_replace("[<]", "＜", $val);
		$val = preg_replace("[>]", "＞", $val);
		$val = preg_replace("[!]", "！", $val);

		$val = preg_replace("[\*]", "＊", $val);
		$val = htmlspecialchars($val);
		$val = str_replace("\\", "￥", $val);

		$val = str_replace('　','  ', $val);
		$val = trim($val);
		$val = str_replace('  ','　', $val);

		return $val;
	}
}
